<template>
  <div>
    <div>
      <div style="display: flex; justify-content: space-between">
        <div>
          <el-input
            clearable
            @clear="initUsrData"
            style="width: 300px; margin-right: 10px"
            type="text"
            v-model="usrName"
            @keydown.enter.native="initUsrData"
            placeholder="请输入用户名进行搜索..."
            prefix-icon="el-icon-search"
          ></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="initUsrData"
            >搜索</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="showAdvanceSearch = !showAdvanceSearch"
          >
            <i
              :class="
                showAdvanceSearch
                  ? 'fa fa-angle-double-up'
                  : 'fa fa-angle-double-down'
              "
              style="margin-right: 5px"
              aria-hidden="true"
            ></i
            >高级搜索</el-button
          >
        </div>
        <div>
          <el-upload
            action="/user/basic/import"
            style="display: inline-block; margin-right: 8px"
            :before-upload="beforeUpload"
            :disabled="importDataDisabled"
            :headers="headers"
            :show-file-list="false"
            :on-success="onSuccess"
            :on-error="onError"
          >
            <el-button
              type="success"
              size="small"
              :icon="importDataIcon"
              :disabled="importDataDisabled"
              >{{ importDataBtn }}</el-button
            >
          </el-upload>
          <el-button
            type="success"
            size="small"
            icon="el-icon-download"
            @click="exportUsr"
            >导出数据</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="showAddUsr"
            >添加用户</el-button
          >
        </div>
      </div>
    </div>
    <el-collapse-transition>
      <div v-show="showAdvanceSearch" class="searchStyle">
        <el-row>
          <el-col :span="3">
            ID:
            <el-input
              v-model="usrSearch.id"
              clearable
              size="mini"
              style="width: 130px"
              placeholder="ID"
            >
            </el-input>
          </el-col>
          <el-col :span="3">
            用户名:
            <el-input
              v-model="usrSearch.name"
              clearable
              size="mini"
              style="width: 130px"
              placeholder="用户名"
            >
            </el-input>
          </el-col>
          <el-col :span="3">
            手机号:
            <el-input
              clearable
              v-model="usrSearch.phone"
              size="mini"
              style="width: 130px"
              placeholder="手机号"
            >
            </el-input>
          </el-col>
          <el-col :span="7">
            注册日期:
            <el-date-picker
              v-model="usrSearch.beginDateScope"
              clearable
              type="daterange"
              unlink-panels
              value-format="yyyy-MM-dd"
              size="mini"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-col>
          <el-col :span="2">
            <el-button
              size="mini"
              icon="el-icon-search"
              type="primary"
              @click="initUsrData('advance')"
              >搜索</el-button
            >
          </el-col>
        </el-row>
      </div>
    </el-collapse-transition>
    <div style="margin-top: 10px">
      <el-table
        :data="usrData"
        show-header
        border
        v-loading="loading"
        element-loading-text="数据加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        style="width: 100%"
      >
        <el-table-column type="selection" width="35"> </el-table-column>
        <el-table-column prop="id" fixed label="#" align="center" width="50">
        </el-table-column>
        <el-table-column label="头像" align="center" width="60">
          <template slot-scope="scope">
            <img :src="scope.row.userFace" width="40" height="40" />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" align="center" width="85">
        </el-table-column>
        <el-table-column prop="account" label="账号" align="center" width="160">
        </el-table-column>
        <el-table-column prop="phone" label="电话" align="center" width="150">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="注册时间"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="lastUpdateTime"
          label="最后修改时间"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="lastLoginIp"
          label="最后登录ip"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column prop="qq" label="QQ号" align="center" width="120">
        </el-table-column>
        <el-table-column
          prop="weiXin"
          label="微信号"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column label="是否启用" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled == 1" type="success">启用</el-tag>
            <el-tag v-if="scope.row.enabled == 0" type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditView(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteUsr(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="display: flex; justify-content: flex-end"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20, 25, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="400px">
      <el-form :model="usr" label-width="84px" :rules="usrRule" ref="userForm">
        <el-form-item label="账号：" prop="account">
          <el-input
            v-model="usr.account"
            clearable
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input
            v-model="usr.password"
            clearable
            show-password
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号：">
          <el-input
            v-model="usr.phone"
            clearable
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名：" prop="name">
          <el-input
            v-model="usr.name"
            clearable
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="头像URL：">
          <el-input
            v-model="usr.userFace"
            clearable
            placeholder="请输入头像URL"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否启用：">
          <el-switch
            v-model="usr.enable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            active-text="启用"
            inactive-text="禁用"
            style="margin-top: 10px"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="QQ号：">
          <el-input
            v-model="usr.qq"
            clearable
            placeholder="请输入QQ号"
          ></el-input>
        </el-form-item>
        <el-form-item label="微信号：">
          <el-input
            v-model="usr.weiXin"
            clearable
            placeholder="请输入微信号（或微信所绑定手机号）"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="usr.remark" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUsr">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "UserBasic",
  data() {
    return {
      usrData: [],
      loading: false,
      defaultProps: {
        children: "children",
        label: "name",
      },
      visible: false,
      visible2: false,
      showAdvanceSearch: false,
      usrSearch: {
        id: null,
        name: null,
        phone: null,
        beginDateScope: null,
      },
      usr: {
        id: null,
        account: null,
        password: null,
        phone: null,
        name: null,
        userFace: "",
        enabled: "1",
        remark: "",
        qq: "",
        weiXin: "",
      },
      usrRule: {
        account: [
          { required: true, min: 1, message: "请输入账号", trigger: "blur" },
        ],
        password: [
          { required: true, min: 1, message: "请输入密码", trigger: "blur" },
        ],
        name: [
          {
            required: true,
            min: 1,
            message: "请输入用户姓名",
            trigger: "blur",
          },
        ],
      },
      currentPage: 1,
      size: 5,
      total: 0,
      usrName: "",
      dialogVisible: false,
      title: "",
      importDataIcon: "el-icon-upload2",
      importDataBtn: "导入数据",
      importDataDisabled: false,
      headers: {
        Authorization: window.sessionStorage.getItem("tokenStr"),
      },
    };
  },
  mounted() {
    this.initUsrData();
  },
  methods: {
    showEditView(data) {
      this.dialogVisible = true;
      this.title = "编辑用户信息";
      this.usr = data;
    },
    deleteUsr(data) {
      this.$confirm(
        "此操作将永久删除【" + data.name + "】, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequest("/user/basic/delete/" + data.id).then((res) => {
            if (res) {
              this.initUsrData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "取消删除",
          });
        });
    },
    addUsr() {
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          if (this.title === "编辑用户信息") {
            this.putRequest("/user/basic/update", this.usr).then((res) => {
              if (res) {
                this.dialogVisible = false;
                this.initUsrData();
              }
            });
          } else {
            this.postRequest("/user/basic/add", this.usr).then((res) => {
              if (res) {
                this.dialogVisible = false;
                this.initUsrData();
              }
            });
          }
        } else {
          this.$message.warning("请完善表单信息！");
        }
      });
    },
    showDept() {
      this.visible = !this.visible;
    },
    showDeptView() {
      this.visible2 = !this.visible2;
    },
    handleNodeClick(data) {
      this.usr.departmentId = data.id;
      this.visible = !this.visible;
    },
    handleNodeClick2(data) {
      this.usrSearch.departmentId = data.id;
      this.visible2 = !this.visible2;
    },
    getMaxId() {
      this.getRequest("/user/basic/maxId").then((res) => {
        if (res) {
          this.usr.id = res.obj;
        }
      });
    },
    beforeUpload() {
      this.importDataBtn = "正在导入";
      this.importDataIcon = "el-icon-loading";
      this.importDataDisabled = true;
    },
    onSuccess() {
      this.importDataBtn = "导入数据";
      this.importDataIcon = "el-icon-upload2";
      this.importDataDisabled = false;
      this.initUsrData();
    },
    onError() {
      this.importDataBtn = "导入数据";
      this.importDataIcon = "el-icon-upload2";
      this.importDataDisabled = false;
    },
    exportUsr() {
      this.downloadRequest("/user/basic/export");
    },
    showAddUsr() {
      this.getMaxId();
      this.title = "添加用户";
      this.usr = {};
      this.dialogVisible = !this.dialogVisible;
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.initUsrData();
    },
    handleSizeChange(size) {
      this.size = size;
      this.initUsrData();
    },
    initUsrData(type) {
      let url =
        "/user/basic/listAll/?currentPage=" +
        this.currentPage +
        "&size=" +
        this.size;
      if (type && type === "advance") {
        if (this.usrSearch.id) {
          url += "&id=" + this.usrSearch.id;
        }
        if (this.usrSearch.name) {
          url += "&name=" + this.usrSearch.name;
        }
        if (this.usrSearch.phone) {
          url += "&phone=" + this.usrSearch.phone;
        }
        if (this.usrSearch.beginDateScope) {
          url += "&dateScope=" + this.usrSearch.beginDateScope;
        }
      } else {
        if (this.usrName) {
          if (this.usrName === "") {
          } else {
            url += "&name=" + this.usrName;
          }
        }
      }
      this.postRequest(url).then((res) => {
        this.loading = true;
        if (res) {
          console.log(res);
          this.loading = false;
          this.usrData = res.data;
          this.total = res.total;
        }
      });
    },
  },
};
</script>

<style scoped>
.deptStyle {
  width: 150px;
  height: 28px;
  font-size: 13px;
  border: 1px solid #dedede;
  display: inline-flex;
  border-radius: 5px;
  box-sizing: border-box;
  padding-left: 15px;
  cursor: pointer;
  align-items: center;
}
.deptSearch {
  width: 130px;
  height: 28px;
  font-size: 13px;
  display: inline-flex;
  border: 1px solid #dedede;
  border-radius: 5px;
  box-sizing: border-box;
  padding-left: 15px;
  cursor: pointer;
  align-items: center;
}
.searchStyle {
  border: 1px solid #409eff;
  border-radius: 5px;
  box-sizing: border-box;
  margin: 10px;
  padding: 5px;
}
</style>
